<%@page import="java.net.URLDecoder"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="java.util.ArrayList"%>
<%@page import="entity.Items"%>
<%@page import="dao.ItemsDao"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>商品详情</title>
</head>
<body>
   <h1>商品详情</h1>
   <hr>
   
   <center>
   
       <table width="750" height="60" cellpadding="0" cellspacing="0" border="0">
          <tr>
             <%
               ItemsDao itemsDao = new ItemsDao();
               Items items = itemsDao.getItemsById(Integer.valueOf(request.getParameter("id")));
               if(items != null) {
             %>
             <td width="70%" valign="top">
                <table>
                   <tr>
                     <td rowspan="4"><img alt="" src="images/<%=items.getPicture() %>" width="200" height="160"></td>
                   </tr>
                   <tr>
                     <td><b><%=items.getName() %></b></td>
                   </tr>
                   <tr>
                     <td>产地：<%=items.getCity() %></td>
                   </tr>
                   <tr>
                     <td>价格：<%=items.getPrice() %>￥</td>
                   </tr>
                </table>
             </td>
             <%
            	} 
             %>
             <%
                String list = "";
                Cookie[] cookies = request.getCookies();
                if(cookies != null && cookies.length > 0) {
	                for(Cookie c: cookies) {
	                	if(c.getName().equals("ListViewCookie")) {
	                		list = URLDecoder.decode(c.getValue(), "utf-8");
	                	}
	                }
                }
                list += request.getParameter("id") + ",";
                System.out.println(list);
                
                // 如果浏览记录超过1000条，清零
                String[] arr = list.split(",");
                if(arr != null && arr.length > 0) {
                	if(arr.length >= 1000)
                		list = "";
                }
                Cookie cookie = new Cookie("ListViewCookie", URLEncoder.encode(list, "utf-8"));
                response.addCookie(cookie);
             %>
             <!-- 浏览过的商品 -->
             <td width="30%" bgcolor="#EEE" align="center">
                <br>
                <b>您浏览过的商品</b><br>
                <%
                   ArrayList<Items> itemsList = itemsDao.getViewList(list);
                   if(itemsList != null && itemsList.size() > 0) {
                	   for(Items item: itemsList) {
                   
                %>
                <div style="display: inline-block;">
                    <dl>
                       <dt>
                         <a href="details.jsp"><img alt="" src="images/<%=item.getPicture()%>" width="120" height="90"></a>
                       </dt>
                       <dd style="-webkit-margin-start:0px"><%=item.getName() %></dd>
                       <dd style="-webkit-margin-start:0px">产地：<%=item.getCity() %>&nbsp;&nbsp;价格：<%=item.getPrice() %>￥</dd>
                    </dl>
                </div>
                <%
                	   }
                   }
                %>
             </td>
          </tr>
       
       </table>
   </center>
</body>
</html>